<?php
namespace Modules\Good\Http\Controllers;
use Modules\Good\Requests\SlideRequest;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Good\Models\Slide;
use Modules\Good\Http\Resources\SlideResource;
class SlideController extends Controller
{
    public function index(){
        $slides=Slide::all();
        return SlideResource::collection($slides);
    }
    public function show($id){
        $slide=Slide::find($id);
        return new SlideResource($slide);
    }
    public function store(SlideRequest $request)
    {
        $data = $request->validated(); // 只获取验证过的数据
        $data['seq'] = (Slide::max('seq') ?? 0) + 1;
        $slide = Slide::create($data);
        return response()->json([
            'message' => 'Slide created successfully',
            'data' => $slide
        ], 201);
    }
    public function update(SlideRequest $request, $id)
    {
        $data = $request->validated(); // 只获取验证过的数据
        $slide = Slide::find($id);
        $slide->update($data);
        return response()->json([
            'message' => 'Slide updated successfully',
            'data' => $slide
        ], 200);
    }
    // 删除轮播图
    public function destroy($id)
    {
        $slide = Slide::find($id);
        if (!$slide) {
            return response()->json(['message' => 'Slide not found'], 404);
        }
        $slide->delete();
        return response()->json(['message' => 'Slide deleted successfully'], 204);
    }
    // 排序
    public function seq(Request $request, $id)
    {
        $slide = Slide::find($id);
        if (!$slide) {
            return response()->json(['message' => 'Slide not found'], 404);
        }
        $slide->seq=$request->input('seq',1);
        $slide->save();
        return response()->json([
            'message' => 'Slide seq updated successfully',
            'data' => $slide
        ], 200);
    }
    

}